{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# shp转geojson"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "from osgeo import ogr,gdal,osr #导入ogr包\n",
    "import os\n",
    "gdal.SetConfigOption(\"GDAL_FILENAME_IS_UTF8\", \"YES\") #中文乱码问题\n",
    "gdal.SetConfigOption(\"SHAPE_ENCODING\", \"GBK\")\n",
    "#set the proj dir 投影数据目录设置\n",
    "os.environ['PROJ_LIB'] = r'.venv\\Lib\\site-packages\\osgeo\\data\\proj'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data\\hubu_create.json\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\GitHub\\py313Env\\.venv\\Lib\\site-packages\\osgeo\\ogr.py:601: FutureWarning: Neither ogr.UseExceptions() nor ogr.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "shp = r\"data\\hubu_create.shp\" #SHP数据路径\n",
    "json = r'data\\hubu_create.json' #输出的json文件路径\n",
    "shp_driver = ogr.GetDriverByName('ESRI Shapefile') # shp数据的数据驱动\n",
    "shp_ds = shp_driver.Open(shp, 0) # 0 means read-only. 1 means writeable.\n",
    "json_driver = ogr.GetDriverByName('GeoJSON') # json文件的数据驱动\n",
    "json_ds=json_driver.CopyDataSource(shp_ds,json) #将shp文件的数据源拷贝到 json的数据源中\n",
    "shp_ds.Destroy() #释放内存\n",
    "print(json_ds.GetDescription()) #测试输出json的数据源的文件描述信息\n",
    "json_ds.Destroy() #释放内存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Geojson to Shp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from osgeo import ogr\n",
    "\n",
    "# 定义输入的GeoJSON文件路径\n",
    "input_geojson_path = r'data\\hubu_create.json'\n",
    "# 定义输出的Shapefile文件路径\n",
    "output_shp_path = r'data\\hubu_create_transfer.shp'\n",
    "\n",
    "# 注册所有的驱动\n",
    "ogr.RegisterAll()\n",
    "\n",
    "# 打开输入的GeoJSON文件\n",
    "input_ds = ogr.Open(input_geojson_path)\n",
    "if input_ds is None:\n",
    "    raise ValueError(f\"无法打开GeoJSON文件: {input_geojson_path}\")\n",
    "\n",
    "# 获取输入文件的图层\n",
    "input_layer = input_ds.GetLayer()\n",
    "\n",
    "# 创建输出的Shapefile文件，设置驱动为ESRI Shapefile\n",
    "driver = ogr.GetDriverByName('ESRI Shapefile')\n",
    "output_ds = driver.CreateDataSource(output_shp_path)\n",
    "if output_ds is None:\n",
    "    raise ValueError(f\"无法创建Shapefile文件: {output_shp_path}\")\n",
    "\n",
    "# 将输入图层的空间参考信息复制到输出Shapefile\n",
    "srs = input_layer.GetSpatialRef()\n",
    "output_layer = output_ds.CreateLayer(output_shp_path, srs=srs)\n",
    "\n",
    "# 遍历输入图层的要素，将其添加到输出图层\n",
    "for feature in input_layer:\n",
    "    output_layer.CreateFeature(feature)\n",
    "\n",
    "# 关闭输入和输出的数据集\n",
    "input_ds.Close()\n",
    "output_ds.Close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
